/**
 * 
 */
package com.iknight.mgr.core.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * @author Administrator
 * @description Ehcache缓存管理工具类
 */
public class EhcacheUtils {

	private CacheManager cacheMgr;

	public CacheManager getCacheMgr() {
		return cacheMgr;
	}

	public void setCacheMgr(CacheManager cacheMgr) {
		this.cacheMgr = cacheMgr;
	}

	public Cache getCache(String cacheName) {
		Cache cache = cacheMgr.getCache(cacheName);
		if (cache == null) {
			throw new RuntimeException("根据cacheName=" + cacheName + "无法获取到对于的cache!请检查配置文件");
		}
		return cache;
	}

	public void put(String cacheName, String key, Object value) {
		Cache cache = getCache(cacheName);
		Element element = new Element(key, value);
		cache.put(element);
	}

	public Object get(String cacheName, String key) {
		Cache cache = cacheMgr.getCache(cacheName);
		Element element = cache.get(key);
		return element == null ? null : element.getObjectValue();
	}

	public void remove(String cacheName, String key) {
		Cache cache = cacheMgr.getCache(cacheName);
		cache.remove(key);
	}
}
